﻿using System;
using Assist;
using Autodesk.Revit.DB;

namespace QuickModel.InteractiveOperation.CmdTestInteractiveOperation
{
	internal class TestCommon
	{
		private bool GetBlockRefName(GeometryObject geoObj, ElementId eleId, ref Element eleSymbol)
		{
			bool flag = geoObj is GeometryElement;
			if (flag)
			{
				GeometryElement geometryElement = geoObj as GeometryElement;
				foreach (GeometryObject geoObj2 in geometryElement)
				{
					bool blockRefName = this.GetBlockRefName(geoObj2, eleId, ref eleSymbol);
					if (blockRefName)
					{
						return true;
					}
				}
			}
			else
			{
				bool flag2 = geoObj is GeometryInstance;
				if (flag2)
				{
					GeometryInstance geometryInstance = geoObj as GeometryInstance;
					bool flag3 = geometryInstance.GraphicsStyleId == eleId;
					if (flag3)
					{
						eleSymbol = geometryInstance.Symbol;
						return true;
					}
					foreach (GeometryObject geoObj3 in geometryInstance.SymbolGeometry)
					{
						bool blockRefName2 = this.GetBlockRefName(geoObj3, eleId, ref eleSymbol);
						if (blockRefName2)
						{
							return true;
						}
					}
				}
			}
			return false;
		}

		public static ModelCurve CreateModelCurve(Curve curCurve, Autodesk.Revit.DB.Document doc, System.Drawing.Color color, int nWideIndex = 1)
		{
			try
			{
				XYZ zero = XYZ.Zero;
				XYZ basisZ = XYZ.BasisZ;
				Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(basisZ, zero);
				SketchPlane sketchPlane = SketchPlane.Create(doc, plane);
				bool flag = curCurve.Length < doc.Application.ShortCurveTolerance;
				if (flag)
				{
					return null;
				}
				ModelCurve modelCurve = doc.Create.NewModelCurve(curCurve, sketchPlane);
				GraphicsStyle graphicsStyle = TestCommon.CreateLineStyle(color, doc, nWideIndex);
				bool flag2 = graphicsStyle != null;
				if (flag2)
				{
					modelCurve.LineStyle = graphicsStyle;
				}
				return modelCurve;
			}
			catch
			{
			}
			return null;
		}

		public static GraphicsStyle CreateLineStyle(System.Drawing.Color color, Autodesk.Revit.DB.Document doc, int nWideIndex = 1)
		{
			string text = "tempLineStyle ";
			text = string.Concat(new string[]
			{
				text,
				color.Red.ToString(),
				"-",
				color.Green.ToString(),
				"-",
				color.Blue.ToString()
			});
			return TestCommon.CreateLineType(doc, text, color, nWideIndex, 1);
		}

		public static GraphicsStyle CreateLineType(Autodesk.Revit.DB.Document doc, string name, System.Drawing.Color color, int nWideIndex, GraphicsStyleType type = 1)
		{
			GraphicsStyle result = null;
			try
			{
				Category category = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);
				bool flag = category.SubCategories.Contains(name);
				Category category2;
				if (flag)
				{
					category2 = category.SubCategories.get_Item(name);
				}
				else
				{
					category2 = doc.Settings.Categories.NewSubcategory(category, name);
				}
				category2.LineColor = color;
				category2.SetLineWeight(nWideIndex, type);
				result = category2.GetGraphicsStyle(type);
			}
			catch
			{
				result = null;
			}
			return result;
		}
	}
}
